﻿// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
// 
// Copyright (c) 2012, Dr. Masroor Ehsan
// All rights reserved.
// 
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//    * Redistributions of source code must retain the above copyright
//      notice, this list of conditions and the following disclaimer.
//    * Redistributions in binary form must reproduce the above copyright
//      notice, this list of conditions and the following disclaimer in the
//      documentation and/or other materials provided with the distribution.
//    * Neither the name of the <organization> nor the
//      names of its contributors may be used to endorse or promote products
//      derived from this software without specific prior written permission.
// 
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
// DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
// DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
// ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
// 
// $Id: Utilities.cs 12 2012-01-27 06:11:46Z masroore@gmail.com $
// 
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Globalization;
using System.Text;

namespace LWObjects
{
    internal static class Utilities
    {
        internal const char ALA_FIELDS_DELIMETER = '\t';
        internal const char ALA_MULTI_SEPARATOR = '|';
        internal const int ALA_FIELDS_COUNT = 256;

        internal static string BoolToStr(this bool value)
        {
            return value ? "Y" : "N";
        }

        internal static bool StrToBool(this string value)
        {
            return (value.Trim().ToUpper() == "Y");
        }

        internal static int StrToInt(this string value)
        {
            int result;
            int.TryParse(value, out result);
            return result;
        }

        internal static string AlaJoinFields(IEnumerable<string> values)
        {
            return AlaJoinFields(values, ALA_FIELDS_DELIMETER);
        }

        internal static string AlaJoinFields(IEnumerable<string> values, char delimeter)
        {
            var sb = new StringBuilder();
            foreach (var item in values)
            {
                if (sb.Length > 0) sb.Append(delimeter);
                sb.Append(item);
            }
            return sb.ToString();
        }

        internal static List<string> AlaSplitFields(string data)
        {
            return AlaSplitFields(data, ALA_FIELDS_DELIMETER);
        }

        internal static List<string> AlaSplitFields(string data, char delimeter)
        {
            var result = new List<string>();
            if (data.IndexOf(delimeter) == -1)
                result.Add(data);
            else
            {
                var parts = data.Split(new[] {delimeter});
                result.AddRange(parts);
            }
            return result;
        }

        internal static List<string> ParseTestResultLinks(string data)
        {
            var result = new List<string>();
            if (data.IndexOf(ALA_MULTI_SEPARATOR) == -1)
                result.Add(data);
            else
            {
                var parts = data.Split(new[] {ALA_MULTI_SEPARATOR});
                result.AddRange(parts);
            }
            return result;
        }

        internal static string ExtractAlaCode(string line)
        {
            var parts = line.Trim().Split(new[] {ALA_FIELDS_DELIMETER});
            return parts.Length > 0 ? parts[0] : string.Empty;
        }

        internal static string EnumToString(Enum value)
        {
            var fi = value.GetType().GetField(value.ToString(CultureInfo.InvariantCulture));
            var attributes = (DescriptionAttribute[]) fi.GetCustomAttributes(typeof (DescriptionAttribute), false);
            return attributes.Length > 0 ? attributes[0].Description : value.ToString(CultureInfo.InvariantCulture);
        }

        internal static object StringToEnum(string value, Type enumType)
        {
            var names = Enum.GetNames(enumType);
            foreach (var name in names)
            {
                if (EnumToString((Enum) Enum.Parse(enumType, name)).Equals(value))
                {
                    return Enum.Parse(enumType, name);
                }
            }

            throw new ArgumentException("The string is not a description or value of the specified enum.");
        }

        internal static string DateToString(DateTime dt)
        {
            return dt.ToString(@"MM/dd/yyyy", CultureInfo.InvariantCulture);
        }

        internal static DateTime StringToDate(string data)
        {
            return DateTime.ParseExact(data, @"MM/dd/yyyy", CultureInfo.InvariantCulture);
        }
    }
}